package com.freshcity.page.home
{
	import com.freshcity.common.MsgInfoBack;
	import com.freshcity.common.WriteMsgWin;
	import com.freshcity.component.container.Component;
	import com.freshcity.component.container.Window;
	import com.freshcity.core.GlobalVar;
	import com.freshcity.core.MainScene;
	import com.freshcity.core.OpCodes;
	import com.freshcity.manager.EffectManager;
	import com.freshcity.manager.LayerManager;
	import com.freshcity.utils.StringUtil;
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.ui.Keyboard;
	
	public class WriteMessage extends Component
	{
		public var callback:Function;
		public var isReply:Boolean;
		public var defaultStr:String="";
		
		private var _uid:String;
		private var _back:WriteMsgWin;
		
		public function WriteMessage()
		{
			this.visible=false;
			super(null, 0, 0);
			this._width=400;
			this._height=300;
			
			back.wordsAmtTip.htmlText="";
			back.words.addEventListener(Event.CHANGE, onWordsTextAreaChangeHandler);
			back.confirmBtn.enabled=true;
			
			//back.closeBtn.addEventListener(MouseEvent.CLICK,hide);
			back.confirmBtn.addEventListener(MouseEvent.CLICK,onSubmit);
		}
		
		protected function onWordsTextAreaChangeHandler(event:Event):void
		{
			var c1:String=StringUtil.trim(back.words.text);
			var loc1:int=StringUtil.getANSILength(c1);
			if (loc1 > 0 && loc1 <= 100)
			{
				var i:int = (100 - loc1) / 2;
				back.wordsAmtTip.htmlText="<font color='#666666'>还可输入" + i + "个字</font>";
				back.confirmBtn.enabled=true;
			}
			else if (loc1 > 100)
			{
				var j:int = (loc1 - 100) / 2;
				back.wordsAmtTip.htmlText="<font color='#FF3603'>超出" + j + "个字</font>";
				back.confirmBtn.enabled=false;
			}
			else
			{
				back.confirmBtn.enabled=false;
			}
		}
		
		protected function onSubmit(event:MouseEvent=null):void
		{
			var s:String = StringUtil.trim(back.words.text)
			if(s.length<2){
				main.msg.showRollTipsMsg("内容过于简单");
			}else{
				main.commu.send(OpCodes.WRITE_MSG,{t:uid,c:s,r:isReply},onWriteMsg);
				back.confirmBtn.enabled=false;
			}
		}
		
		private function onWriteMsg(data:*):void
		{
			main.commu.unRegister(OpCodes.WRITE_MSG);
			this.dispatchEvent(new Event(Event.COMPLETE));
			
			this.isReply=false;
			back.confirmBtn.enabled = true;
			if(data is String){
				main.msg.showRollTipsMsg(data);
			}else{
				var msg:String;
				if(data is int){
					msg = "发送成功。今日可主动留言数减少为"+data+"条。";
				}else{
					msg = "信息已成功发出";
				}
				
				main.msg.showRollTipsMsg(msg);
				back.words.text="";
				main.commu.send(OpCodes.TALK_HISTORY,{u:uid},showMessage);
				if(this.callback!=null){
					TweenLite.delayedCall(1,excuteCallBack);
				}
			}
		}
		
		private function excuteCallBack():void
		{
			this.callback.call();
			this.callback=null;
			this.hide();
		}
		
		override protected function addChildren():void
		{
			super.addChildren();
			_back = new WriteMsgWin();
			addChild(_back);
			/*
			var tf:TextField = StringUtil.addHyLink("（个人资料）",_uid,main.onAboutUser);
			tf.x = 180;
			tf.y = 15;
			_back.addChild(tf);
			*/
		}
		
		override public function draw():void
		{
			super.draw();
		}

		public function get uid():String
		{
			return _uid;
		}

		public function set uid(value:String):void
		{
			_uid = value;
			main.commu.send(OpCodes.TALK_HISTORY,{u:uid},showMessage);
		}
		
		private function showMessage(data:*):void
		{
			main.commu.unRegister(OpCodes.TALK_HISTORY);
			var msgBox:Sprite = new Sprite();
			msgBox.mouseEnabled = false;
			
			var tempY:int=0;
			var whoDesc:String;
			
			for each (var o:* in data) 
			{
				var s:MsgInfoBack = new MsgInfoBack();
				s.y = tempY;
				
				s.from.text = StringUtil.whoDesc(o.from_user) + ":";
				s.message.text = o.content;
				s.createAt.text = StringUtil.formatDate(o.created_at);
				
				msgBox.addChild(s);
				tempY += 50;
			}
			
			_back.panel.source=msgBox;
		}
		
		public function onShow(parent:DisplayObjectContainer=null):void
		{
			if(this.parent!=null) this.parent.removeChild(this);
			back.words.text = this.defaultStr;
			if(parent){
				parent.addChild(this);
				this.x = 40;
				this.y = 10;
				super.show();
			}else{
				var win:Window = new Window(null,0,0,"写信");
				win.hasCloseButton=true;
				win.addChild(this);
				this.x = 0;
				this.y = 0;
				win.layer = LayerManager.popupLayer;
				win.layer.addPopUp(win);
				win.setSize(400,340);
				//EffectManager.addUIMask([],0);
				this.visible=true;
				win.show();
			}
			
			main.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
		}
		
		protected function onKeyDown(evt:KeyboardEvent):void
		{
			if (evt.keyCode == Keyboard.ENTER && back.words.text.length > 0)
			{				
				onSubmit();
			}
		}
		
		override public function destroy():void
		{
			EffectManager.hideUIMask();
			if(this.parent)this.parent.removeChild(this);
		}
		
		override public function hide(event:Event=null):void
		{
			if(this.parent.parent is Window){
				Window(this.parent.parent).hide();
			}
			super.hide(event);
		}
		
		public function get back():WriteMsgWin
		{
			return _back;
		}

		public function set back(value:WriteMsgWin):void
		{
			_back = value;
		}

		
	}
}